package com.wangdong.ceshirenlianhangxin.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sensetime.senseid.sdk.liveness.silent.common.app.CameraActivity;
import com.wangdong.ceshirenlianhangxin.MainActivity;
import com.wangdong.ceshirenlianhangxin.R;

public class StartPageFragment extends BaseFragment{

    private TextView mErrorMessageView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_start_page, container, false);
        mErrorMessageView = (TextView)view.findViewById(R.id.txt_error);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        
        if (getArguments() != null) {
            showErrorMessage(getArguments().getInt(MainActivity.EXTRA_RESULT_CODE, -1));
        }
    }

    private void showErrorMessage(int resultCode) {
        int messageCode = -1;
        switch (resultCode) {
            case CameraActivity.RESULT_CANCELED:
                messageCode = R.string.txt_error_canceled;
                break;
            case CameraActivity.RESULT_CODE_CAMERA_ERROR:
                messageCode = R.string.txt_error_permission;
                break;
            case CameraActivity.RESULT_CODE_NO_PERMISSIONS:
                messageCode = R.string.txt_error_permission;
                break;
            case CameraActivity.RESULT_CODE_TIMEOUT:
                messageCode = R.string.txt_error_timeout;
                break;
            case CameraActivity.RESULT_CODE_MODEL_FILE_NOT_FOUND:
                messageCode = R.string.txt_error_model_not_found;
                break;
            case CameraActivity.RESULT_CODE_LICENSE_FILE_NOT_FOUND:
                messageCode = R.string.txt_error_license_not_found;
                break;
            case CameraActivity.RESULT_CODE_CHECK_LICENSE_FAIL:
            case CameraActivity.RESULT_CODE_SDK_VERSION_MISMATCH:
            case CameraActivity.RESULT_CODE_PLATFORM_NOTSUPPORTED:
                messageCode = R.string.txt_error_license;
                break;
            case CameraActivity.RESULT_CODE_CHECK_MODEL_FAIL:
                messageCode = R.string.txt_error_model;
                break;
            case CameraActivity.RESULT_CODE_LICENSE_EXPIRE:
                messageCode = R.string.txt_error_license_expire;
                break;
            case CameraActivity.RESULT_CODE_LICENSE_PACKAGE_NAME_MISMATCH:
                messageCode = R.string.txt_error_license_package_name;
                break;
            case CameraActivity.RESULT_CODE_WRONG_STATE:
                messageCode = R.string.txt_error_state;
                break;
            case CameraActivity.RESULT_CODE_ERROR_API_KEY_SECRET:
                messageCode = R.string.error_api_key_secret;
                break;
            case CameraActivity.RESULT_CODE_SERVER:
                messageCode = R.string.error_server;
                break;
            case CameraActivity.RESULT_CODE_DETECT_FAIL:
                messageCode = R.string.txt_detect_fail;
                break;
            default:
                messageCode = -1;
                break;
        }
        if (messageCode > -1) {
            mErrorMessageView.setVisibility(View.VISIBLE);
            mErrorMessageView.setText(messageCode);
        } else {
            mErrorMessageView.setVisibility(View.GONE);
        }
    }
}